﻿using Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using SuYunExpressPackageSystem.BLL;
using SuYunExpressPackageSystem.Models.UIModels;
using SuYunExpressPackageSystem.Utility;

namespace SuYunExpressPackageSystem.Stat
{
    public partial class FrmExpCompletionStatistics : Form
    {
        public FrmExpCompletionStatistics()
        {
            InitializeComponent();
        }
        StationBLL stationBLL = new StationBLL();
        ExpStatisticsBLL statisticsBLL = new ExpStatisticsBLL();
        private void FrmExpCompletionStatistics_Load(object sender, EventArgs e)
        {
            //站点下拉框加载
            FormUtility.LoadCboStations(cboStations, stationBLL);
            cboStations.SelectedIndex = 0;
            //统计当前的完成度
            //默认加载已完成的列表
            dgvExpList.AutoGenerateColumns = false;
            StatisticsCompletionCount();
            
        }

        /// <summary>
        /// 统计快递完成度数据
        /// </summary>
        private void StatisticsCompletionCount()
        {
            int stationId = cboStations.SelectedValue.GetInt();
            List<ExpressCountInfo> countData = statisticsBLL.StatExpressCountData(stationId);
            if(countData!=null)
            {
                chartExpStat.DataSource = countData;
                Series seriesComplete = chartExpStat.Series[0];
                seriesComplete.Label = "#PERCENT{P}";//显示百分比
                seriesComplete.XValueType = ChartValueType.String;//x轴的值的类型
                seriesComplete.YValueType = ChartValueType.Int32;//y轴的值的类型
                seriesComplete["PieLableStyle"] = "OutSide";//设置标签文本显示在图标外边
                seriesComplete.BorderWidth = 2;
                seriesComplete.XValueMember = "ExpState";
                seriesComplete.YValueMembers = "ExpCount";
                seriesComplete.LegendText = "#VALX";//图例文本

                lblHasCompleCount.Text = countData[0].ExpCount.ToString();
                lblUnCompleteCount.Text = countData[1].ExpCount.ToString();
            }

            LoadExpressListByCount(true,stationId);
        }

        /// <summary>
        /// 加载指定的快递列表
        /// </summary>
        /// <param name="isCompleted"></param>
        /// <param name="stationId"></param>
        private void LoadExpressListByCount(bool isCompleted,int stationId)
        {
            List<ExpressComInfo> expList = statisticsBLL.GetExpressListByCount(isCompleted, stationId);
            if (expList.Count > 0)
            {
                dgvExpList.DataSource = expList;
            }
            else
                dgvExpList.DataSource = null;
        }

        private void lblHasCompleCount_Click(object sender, EventArgs e)
        {
            int stationId = cboStations.SelectedValue.GetInt();
            LoadExpressListByCount(true, stationId);
        }

        private void lblUnCompleteCount_Click(object sender, EventArgs e)
        {
            int stationId = cboStations.SelectedValue.GetInt();
            LoadExpressListByCount(false, stationId);
        }

        private void btnStatistics_Click(object sender, EventArgs e)
        {
            StatisticsCompletionCount();
        }
    }
}
